package mest.service.hws.impl;

import com.alibaba.fastjson.JSON;
import mest.dao.hws.plan_productjob_purchase_recordMapper;
import mest.entity.plan_productjob_purchase;
import mest.entity.plan_productjob_purchase_record;
import mest.service.hws.plan_productjob_purchaseService;
import mest.service.hws.plan_productjob_purchase_recordService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional(rollbackFor=Exception.class)
public class plan_productjob_purchase_recordServiceimpl implements plan_productjob_purchase_recordService {


    @Resource
    private plan_productjob_purchase_recordMapper pppr;

    @Resource
    private plan_productjob_purchaseService ppp;
    //新增操作记录
    @Override
    public String insert(String theirDb,
                         List<plan_productjob_purchase_record> record, String time) {
        /*/insertModel /delModel /updateByone*/
        System.out.println("进入增删改方法");
        Map<String, Object> map = new HashMap<String, Object>();
        try {
                for (plan_productjob_purchase_record record1:record){
                    //传入参数的对象的id使操作记录的pppid
                    record1.setPppId(record1.getId());
                }
                //新增操作记录
                int i = pppr.insert(theirDb,record);
                //变更采购内容单的已下达数量
                int j = ppp.updateByone(theirDb,record);
              System.out.println("新增操作记录"+i+"条，修改内容表下达数量"+j+"条");
                map.put("code", 0);
                map.put("msg", "操作成功");
                map.put("count", i);
        }catch (Exception e){
            map.put("code", 1);
            map.put("msg", "操作失败");
            System.out.println(e.toString());
        }    
        return JSON.toJSONString(map);
    }

    //根据id查询操作记录
    @Override
    public String selectByPrimaryKey(String theirDb,
                                     Integer id) {
        /* selectById  selectAll  */
        System.out.println("进入查询");
        Map<Object,Object> map = new HashMap<Object,Object>();
        try {
                map.put("code",0);
                map.put("msg","查询成功");
                map.put("count",(pppr.selectByPrimaryKey(theirDb,id)==null)?0:1);
                map.put("data",pppr.selectByPrimaryKey(theirDb,id));
        }catch (Exception e){
                System.out.println(e.toString());
                map.put("code",1);
                map.put("msg","查询失败");
                map.put("errmsg",e.toString());
        }
        return JSON.toJSONString(map);
    }

    //查询一张计划单内容的所有操作记录
    @Override
    public Map<String, Object> selectPrimary(String theirDb,
                                Integer pppId, Integer limit, Integer page) {
        /* selectById  selectAll  */
        System.out.println("进入查询");
        Map<String,Object> map = new HashMap<String,Object>();
        try {
                map.put("code",0);
                map.put("msg","查询成功");
                map.put("count",pppr.selectcount(theirDb,pppId));
                map.put("data",pppr.selectPrimary(theirDb,pppId, limit, (page-1)*limit));
        }catch (Exception e){
                System.out.println(e.toString());
                map.put("code",1);
                map.put("msg","查询失败");
                map.put("errmsg",e.toString());
        }
        return map;
    }

    //更新记录表
    @Override
    public String updateByPrimaryKey(String theirDb,
                                     plan_productjob_purchase_record record) {
        /*/insertModel /delModel /updateByone*/
        System.out.println("进入增删改方法");
        Map<String, Object> map = new HashMap<String, Object>();
        try {
                map.put("code", 0);
                map.put("msg", "操作成功");
                map.put("count", pppr.updateByPrimaryKey(theirDb,record));
        }catch (Exception e){
            map.put("code", 1);
            map.put("msg", "操作失败");
            e.toString();
        }    
        return JSON.toJSONString(map);
    }
}
